<template>
  <div class="container">
    <div class="search-box">
      <el-input placeholder="请输入内容" class="input-box" v-model="searchText"></el-input>
      <el-button @click="clearText">清除</el-button>
      <el-button type="primary" @click="searchTask">搜索</el-button>
    </div>
    <el-table :data="list" border style="width: 100%">
      <el-table-column type="index" label="任务编号" width="80"> </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template slot-scope="scope">
          <el-button size="mini" style="margin-right: 10px" @click="editTask(scope.row)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="delTask(scope.row)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination layout="prev, pager, next" :total="50" @current-change="updateCurrentChange">
      </el-pagination>
      <el-button size="small" plain @click="addTask">+ 新增</el-button>
    </div>
    <!-- 新增弹框 -->
    <el-dialog
      :title="formData.id ? '新增' : '编辑'"
      :visible.sync="dialogVisible"
      width="30%"
      @close="closeDialog"
    >
      <el-form ref="form" label-width="80px" :model="formData" :rules="rules">
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="formData.name"></el-input>
        </el-form-item>
        <el-form-item label="任务描述" prop="des">
          <el-input v-model="formData.des"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  fetchTaskList,
  fetchAddTask,
  fetchEditTask,
  fetchTaskDetail,
  fetchDelTask,
  fetchSearchTask
} from '@/api/user.js'
export default {
  data() {
    return {
      list: [],
      params: {
        _page: 1, // 当前第几页
        _limit: 5 // 每页条数
      },
      dialogVisible: false,
      formData: {
        name: '',
        des: '',
        id: ''
      },
      rules: {
        name: [{ required: true, message: '任务名称必填', trigger: 'blur' }],
        des: [{ required: true, message: '任务名称必填', trigger: 'blur' }]
      },
      searchText: ''
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    //渲染列表
    async getList() {
      const res = await fetchTaskList(this.params)
      console.log(res)
      this.list = res
    },
    //动态分页渲染
    updateCurrentChange(val) {
      this.params._page = val
      this.getList()
    },
    addTask() {
      this.dialogVisible = true
    },
    closeDialog() {
      this.dialogVisible = false
      this.formData = {
        formData: {
          name: '',
          des: '',
          id: ''
        }
      }
    },
    //确定按钮，兜底校验
    confirm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (this.formData.id) {
            //console.log('编辑')
            await fetchEditTask(this.formData)
          } else {
            await fetchAddTask(this.formData)
          }
          this.$message.success(`${this.formData.id ? '编辑成功' : '新增成功'}`)
          this.getList()
          this.$refs.form.resetFields()
          this.closeDialog()
        }
      })
    },
    async editTask(row) {
      this.dialogVisible = true
      const res = await fetchTaskDetail(row)
      this.formData = res
    },
    async delTask(row) {
      await fetchDelTask(row)
      this.$message.success('删除成功')
      this.getList()
    },
    async searchTask() {
      const res = await fetchSearchTask(this.searchText)
      this.list = res
    },
    clearText() {
      this.searchText = ''
      this.getList()
    }
  }
}
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
